package com.shanzhu.oe.controller;



import com.shanzhu.oe.common.R;
import com.shanzhu.oe.service.AIService;
import com.shanzhu.oe.util.ApiResultHandler;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import reactor.core.publisher.Flux;


import javax.annotation.Resource;
import java.util.HashMap;

@RestController
@RequestMapping("/ai")
public class AIController {
    @Resource
    private RestTemplate restTemplate;
    @Value("${ai.url}")
    private  String url;
    //deepseek模型
    @Value("${ai.deepSeek.model}")
    private String  deepSeekModel;
    @Value("${ai.deepSeek.stream}")
    private Boolean deepSeekStream;
    //通义模型
    @Value("${ai.qwen.model}")
    private String  qwenModel;
    @Value("${ai.qwen.stream}")
    private Boolean qwenStream;

    @GetMapping("/deepSeek")
    public R requestAI(String prompt) {
        //创建请求头对象
        HttpHeaders headers = new HttpHeaders();
        //设置请求的数据格式为JSON
        headers.setContentType(MediaType.APPLICATION_JSON);
        //创建请求的参数对象
        HashMap<String,Object>  parameter = new HashMap<>();
        //添加参数  当系统中只有一个模型的时候 直接在Yml里面配置  但是如果有多个  那么就是每个接口定义  或者直接定义多个model
        parameter.put("prompt",prompt);
        parameter.put("model", deepSeekModel);
        parameter.put("stream", deepSeekStream);
        //创建请求对象
        HttpEntity<HashMap<String,Object>> request = new HttpEntity<>(parameter, headers);
        //发送post请求 并且获得响应的结果集
        String result = restTemplate.postForObject(
                url,
                request,
                String.class
        );
        System.out.println(result);
        return ApiResultHandler.success(result);
    }



    @Resource
    private AIService aiService;
    @PostMapping("/qwen")
    public Flux<String> qwen(@RequestBody  String prompt) {
        return aiService.streamQuery(prompt);
    }

}
